package org.bits.common.ui;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.bits.common.model.ItemTableModel;
import org.jutils.ui.event.ItemActionList;
import org.jutils.ui.event.ItemActionListener;

/*******************************************************************************
 * @param <T>
 ******************************************************************************/
public class ItemListPanel<T>
{
    /**  */
    private JPanel panel;
    /**  */
    private JTable itemsTable;

    /**  */
    private ItemTableModel<T> itemsModel;
    /**  */
    private ItemActionList<T> itemSelectedListeners;
    /**  */
    private ItemActionList<T> editItemListeners;

    /***************************************************************************
     * @param model
     **************************************************************************/
    public ItemListPanel( ItemTableModel<T> model )
    {
        itemsModel = model;
        itemSelectedListeners = new ItemActionList<T>();
        editItemListeners = new ItemActionList<T>();
        panel = new JPanel( new GridBagLayout() );

        itemsTable = new JTable( itemsModel );

        JScrollPane scrollPane = new JScrollPane( itemsTable );

        itemsTable.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
        itemsTable.getSelectionModel().addListSelectionListener(
            new ItemSelectedListener() );
        itemsTable.addMouseListener( new TableMouseListener() );

        GridBagConstraints constraints = new GridBagConstraints( 0, 0, 1, 1,
            1.0, 1.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH,
            new Insets( 0, 4, 4, 4 ), 0, 0 );
        panel.add( scrollPane, constraints );
    }

    /***************************************************************************
     * @param l
     **************************************************************************/
    public void addItemSelectedListener( ItemActionListener<T> l )
    {
        itemSelectedListeners.addListener( l );
    }

    /***************************************************************************
     * @param l
     **************************************************************************/
    public void addEditItemListener( ItemActionListener<T> l )
    {
        editItemListeners.addListener( l );
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    public JTable getTable()
    {
        return itemsTable;
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    public JComponent getView()
    {
        return panel;
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class TableMouseListener extends MouseAdapter
    {
        @Override
        public void mouseClicked( MouseEvent e )
        {
            if( e.getClickCount() == 2 )
            {
                int row = itemsTable.getSelectedRow();

                if( row > -1 )
                {
                    T t = itemsModel.getItem( row );
                    editItemListeners.fireListeners( ItemListPanel.this, t );
                }
            }
        }
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class ItemSelectedListener implements ListSelectionListener
    {
        @Override
        public void valueChanged( ListSelectionEvent e )
        {
            if( !e.getValueIsAdjusting() )
            {
                int row = itemsTable.getSelectedRow();
                if( row > -1 )
                {
                    T item = itemsModel.getItem( row );
                    itemSelectedListeners.fireListeners( ItemListPanel.this,
                        item );
                }
            }
        }
    }
}
